package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.financing.platform.common.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.CsvExportable;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ActivitiesGroupDTO;
import com.financing.platform.service.IActivitesGroupService;
import com.financing.platform.util.CsvExportUtil;

@Controller("activitiesGroupAction")
@RequestMapping("/api/activitiesGroup")
@Scope("prototype")
public class ActivitiesGroupAction extends BaseAction{
	
	@Autowired
	private IActivitesGroupService activitesGroupService;
	
	/**
	 *  组团活动列表
	 * @param request
	 * @param response
	 * @param agd
	 * @return
	 */
	@RequestMapping("/queryActivitiesGroupInfo.shtml")
	@ResponseBody
	public ResultMap queryActivitiesGroupInfo(HttpServletRequest request,
			HttpServletResponse response,ActivitiesGroupDTO agd,Pager page) {
		try{
			log.info("Begin queryActivitiesGroupInfo list!");

			agd.setBegin(page.getStartIndex());
			agd.setPageSize(page.getPageSize());

			int count = activitesGroupService.queryActivitiesGroupInfoCount(agd);
			page.setTotalCount(count);
			
			List<ActivitiesGroupDTO> list = activitesGroupService.queryActivitiesGroupInfo(agd);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", page);

			log.info("End queryActivitiesGroupInfo list!");
		}catch(Exception e){
			log.error("queryActivitiesGroupInfo error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	
	
	
	/**
	 * 组团活动详情
	 * @param request
	 * @param response
	 * @param agd
	 * @return
	 */
	@RequestMapping("/queActivitiesGroupDetail.shtml")
	@ResponseBody
	public ResultMap queActivitiesGroupDetail(HttpServletRequest request,
			HttpServletResponse response,ActivitiesGroupDTO agd) {
		try{
			log.info("Begin queActivitiesGroupDetail list!");
			
			//查询团信息
			List<ActivitiesGroupDTO> listGroup = activitesGroupService.queryActivitiesGroupInfo(agd);
			
			//查询团成员信息
			List<ActivitiesGroupDTO> listMember = activitesGroupService.queryActivitiesGroupMemberInfo(agd);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("listGroup",listGroup);
			resultMap.setAttribute("listMember",listMember);
			
			log.info("End queActivitiesGroupDetail list!");
		}catch(Exception e){
			log.error("queActivitiesGroupDetail error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	
	/**
	 * 组团 团激活
	 * @param request
	 * @param response
	 * @param agd
	 * @return
	 */
	@RequestMapping("/activationGroup.shtml")
	@ResponseBody
	public ResultMap activationGroup(HttpServletRequest request,
			HttpServletResponse response,ActivitiesGroupDTO agd) {
		try{
			log.info("Begin activationGroup list!");
			
			List<ActivitiesGroupDTO> listGroup = activitesGroupService.queryActivitiesGroupInfo(agd);
			
			ActivitiesGroupDTO activitiesGroup = listGroup.get(0);
			if(activitiesGroup.getFpGroupTotalMember().intValue()>activitiesGroup.getFpGroupSuccessMember().intValue()){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_AUTH_MEMBER_NOT_ENOUGH);
				return resultMap;
			}
			
			activitesGroupService.updateActivitiesGroup(agd);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End activationGroup list!");
		}catch(Exception e){
			log.error("activationGroup error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	
	/**
	 *  组团活动列表导出cvs
	 * @param request
	 * @param response
	 * @param agd
	 * @return
	 */
	@RequestMapping("/queryActivitiesGroupInfoCSV.shtml")
	@ResponseBody
	public void queryActivitiesGroupInfoCSV(HttpServletRequest request,
			HttpServletResponse response,ActivitiesGroupDTO agd) {
		try{
			log.info("Begin queryActivitiesGroupInfoCSV list!");
			
			List list = activitesGroupService.queryActivitiesGroupInfo(agd);
			CsvExportUtil.doCsvExport(response, list);
			
			log.info("End queryActivitiesGroupInfoCSV list!");
		}catch(Exception e){
			log.error("queryActivitiesGroupInfoCSV error !",e);
		}
	}
}
